package zhihui.mobile.ui.express;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.zgm.utils.ui.UiTextHeader;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import zhihui.mobile.R;
import zhihui.mobile.bean.address.Area;
import zhihui.mobile.bean.address.City;
import zhihui.mobile.bean.address.Province;
import zhihui.mobile.bean.address.UiData;
import zhihui.mobile.manager.address.AddressManager;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.task.AreaLoadTask;
import zhihui.mobile.ui.BaseFragmentActivity;
import zhihui.mobile.ui.MyOnClickListener;
import zhihui.mobile.ui.address.adapter.AreaAdapter;

/**
 * Created by Administrator on 2017/9/16.
 */

public class SendOffExpressActivity extends BaseFragmentActivity {
    @Bind(R.id.express_send_off_select_address_tv)
    TextView expressSendOffSelectAddressTv;
    @Bind(R.id.express_history_title_layout)
    UiTextHeader expressHistoryTitleLayout;

    private int provincePosition;
    private int cityPosition;


    @Override
    public void initView() {


    }

    @Override
    protected int getLayoutResId() {
        return R.layout.exprees_send_off_activity;
    }

    @Override
    protected IPresenter[] getPresenters() {
        return new IPresenter[0];
    }

    @Override
    protected void onInitPresenter() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        //title数据
        expressHistoryTitleLayout.setTitle("寄包裹");
        expressHistoryTitleLayout.setRightText("");
        expressHistoryTitleLayout.setLeftBottonIcon(R.drawable.shop_back);
        expressHistoryTitleLayout.setLeftButtonOnClickListener(new MyOnClickListener(this) {
            @Override
            public void onClick(View v) {
                super.onClick(v);
                finish();
            }
        });

    }


    @OnClick({R.id.express_send_off_select_address_tv, R.id.express_send_off_pay_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.express_send_off_select_address_tv:
                //显示地址列表
                addressDialog();
                break;
            case R.id.express_send_off_pay_btn:
                break;
        }
    }


    /*选择地址弹窗*/
    private void addressDialog() {
        if (AddressManager.getInstance().getmAddressList() == null || AddressManager.getInstance().getmAddressList().size() == 0) {
            new AreaLoadTask(this, new AreaLoadTask.ICallBackListener() {
                @Override
                public void finish(boolean result) {
                    if (result) {
                        getAddress(AddressManager.getInstance().getmAddressList());
                    }
                }
            }).execute();
        } else {
            getAddress(AddressManager.getInstance().getmAddressList());
        }
    }

    /*选择地址数据*/
    private void getAddress(final List<Province> list) {
        provincePosition = 0;
        cityPosition = 0;
        final Dialog dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(this).inflate(R.layout.address_dialog, null);
        ListView lv_province = (ListView) view.findViewById(R.id.lv_province);
        final AreaAdapter provinceAdapter = new AreaAdapter(this);
        provinceAdapter.setmList(list);
        lv_province.setAdapter(provinceAdapter);
        lv_province.setSelection(0);

        final List<City> cityList = new ArrayList<>();
        cityList.addAll(list.get(0).getCity());
        ListView lv_city = (ListView) view.findViewById(R.id.lv_city);
        final AreaAdapter cityAdapter = new AreaAdapter(this);
        cityAdapter.setmList(cityList);
        lv_city.setAdapter(cityAdapter);
        lv_city.setSelection(0);

        final List<Area> areaList = new ArrayList<>();
        areaList.addAll(cityList.get(0).getArea());
        ListView lv_area = (ListView) view.findViewById(R.id.lv_area);
        final AreaAdapter areaAdapter = new AreaAdapter(this);
        areaAdapter.setmList(areaList);
        lv_area.setAdapter(areaAdapter);
        lv_area.setSelection(0);

        lv_province.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cityList.clear();
                cityList.addAll(list.get(position).getCity());
                cityAdapter.setmList(cityList);
                cityAdapter.notifyDataSetChanged();
                areaAdapter.setmList(new ArrayList<UiData>());
                areaAdapter.notifyDataSetChanged();
                provincePosition = position;
                expressSendOffSelectAddressTv.setText(list.get(provincePosition).getName());

            }
        });

        lv_city.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                areaList.clear();
                areaList.addAll(cityList.get(position).getArea());
                areaAdapter.setmList(areaList);
                areaAdapter.notifyDataSetChanged();
                cityPosition = position;
                expressSendOffSelectAddressTv.setText(list.get(provincePosition).getName() + cityList.get(cityPosition).getName());

            }
        });

        lv_area.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                expressSendOffSelectAddressTv.setText(list.get(provincePosition).getName() + cityList.get(cityPosition).getName() + areaList.get(position).getName());
                dialog.dismiss();
                dialog.cancel();
            }
        });
        //布局给Dialog
        dialog.setContentView(view);
        //获取当前Activity窗体
        Window window = dialog.getWindow();
        //设置dialog 从底部弹出
        window.setGravity(Gravity.BOTTOM);
        //获得窗体属性
        WindowManager.LayoutParams params = window.getAttributes();
        params.y = 0;
        window.setAttributes(params);
        dialog.show();
    }

}
